﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Xww.Text.Process {
    [Serializable]
    public abstract class LineProcessorBase : IProcessLine {

        protected LineProcessorBase() {
            IgnoreCase = false;
            Proceed = true;
            InverseMatch = false;
        }

        #region IProcessLine 成员

        public virtual string Process(string input) {
            if (MatchOnly) {
                if (Match(input)) { return null; }
                else {
                    return input;
                }
            }
            else {
                return TrueProcess(input);
            }
        }

        public virtual bool Match(string input) {
            return InverseMatch ? !TrueMatch(input) : TrueMatch(input);
        }

        public bool IfProceed {
            get;
            set;
        }

        public bool MatchOnly {
            get;
            set;
        }

        #endregion

        public abstract MatchType MatchType { get;  }
        public bool IgnoreCase { get; protected set; }
        public string InputPattern { get; protected set; }
        public string OutputPattern { get;protected set; }
        public bool Proceed { get; set; }
        public bool InverseMatch { get; set; }
        public abstract bool TrueMatch(string input);
        public abstract string TrueProcess(string input);

        public override string ToString() {
            if (MatchOnly) {
                return string.Format("【Ignore】 【{0}】 【{1}】", MatchType, InputPattern);
            }
            return string.Format("【{0}】 【{1}】 【{2}】", MatchType, InputPattern, 
                System.Text.RegularExpressions.Regex.Escape( OutputPattern ));
        }
    }
}
